package com.lonely;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 黄志标
 * @date 2023/4/14 13:42
 * @Description: https://leetcode.cn/problems/remove-duplicates-from-sorted-list/
 * <p>
 * 给定一个已排序的链表的头 head ， 删除所有重复的元素，使每个元素只出现一次 。返回 已排序的链表 。
 */
public class Question83_删除排序链表中的重复元素 {


    public static void main(String[] args) {

        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(1);
        ListNode node3 = new ListNode(1);

        node1.setNext(node2);
        node2.setNext(node3);

        ListNode result = deleteDuplicates(node1);
        System.out.println(result);

    }

    /**
     * 思路：
     *
     * @param head
     * @return
     */
    public static ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }
        if (head.val == head.next.val) {
            head = deleteDuplicates(head.next);
        } else {
            head.next = deleteDuplicates(head.next);
        }
        return head;
    }


    @Data
    private static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
